﻿using Nop.Core.Domain.XJ;
using Nop.Services.Common;
using Nop.Services.Customers;
using Nop.Services.Directory;
using Nop.Services.Logging;
using Nop.Services.Media;
using Nop.Services.XJ;
using System;
using System.Web.Mvc;

namespace Nop.Web.Controllers
{
	public class PlaceSignController : Controller
	{
		#region Fields
		private readonly IDepartmentService _departmentService;
		private readonly ICustomerService _customerService;
		private readonly IFilesService _filesService;
		private readonly IPictureService _pictureService;
		private readonly IMessageService _messageService;
		private readonly ILogger _logger;
		private readonly IGenericAttributeService _genericAttributeService;
		private readonly IXJPlaceService _xjPlaceService;
		private readonly IXJPlaceSignService _xjPlaceSignService;
		private readonly IXJEquipmentCategoryService _xJEquipmentCategoryService;
		private readonly IXJEquipmentService _xJEquipmentService;
		private readonly IXJEquipmentStockService _xJEquipmentStockService;
		private readonly IXJInspectionService _xJInspectionService;
		private readonly IXJInspectionPlanService _xJInspectionPlanService;
		private readonly IXJMaintenanceService _xJMaintenanceService;
		private readonly IXJMaintenanceCommentService _xJMaintenanceCommentService;
		#endregion

		#region Ctor
		public PlaceSignController(
			IDepartmentService departmentService,
			ICustomerService customerService,
			IFilesService filesService,
			IPictureService pictureService,
			IMessageService messageService,
			ILogger logger,
			IGenericAttributeService genericAttributeService,
			IXJPlaceService xjPlaceService,
			IXJPlaceSignService xjPlaceSignService,
			IXJEquipmentCategoryService xJEquipmentCategoryService,
			IXJEquipmentService xJEquipmentService,
			IXJEquipmentStockService xJEquipmentStockService,
			IXJInspectionService xJInspectionService,
			IXJInspectionPlanService xJInspectionPlanService,
			IXJMaintenanceService xJMaintenanceService,
			IXJMaintenanceCommentService xJMaintenanceCommentService)
		{
			this._departmentService = departmentService;
			this._customerService = customerService;
			this._filesService = filesService;
			this._pictureService = pictureService;
			this._messageService = messageService;
			this._logger = logger;
			this._genericAttributeService = genericAttributeService;
			this._xjPlaceService = xjPlaceService;
			this._xjPlaceSignService = xjPlaceSignService;
			this._xJEquipmentCategoryService = xJEquipmentCategoryService;
			this._xJEquipmentService = xJEquipmentService;
			this._xJEquipmentStockService = xJEquipmentStockService;
			this._xJInspectionService = xJInspectionService;
			this._xJInspectionPlanService = xJInspectionPlanService;
			this._xJMaintenanceService = xJMaintenanceService;
			this._xJMaintenanceCommentService = xJMaintenanceCommentService;
		}
		#endregion

		#region Utilities

		#endregion

		/// <summary>
		/// 巡检点打卡
		/// </summary>
		/// <param name="customerId"></param>
		/// <param name="placeId"></param>
		/// <param name="longitude"></param>
		/// <param name="latitude"></param>
		/// <returns></returns>
		[HttpPost]
		public JsonResult Sign(int customerId, int placeId, string longitude = "", string latitude = "")
		{
			if (customerId == 0)
				return Json(new { result = false, msg = "用户无效" });

			var place = _xjPlaceService.GetXJPlaceById(placeId);
			if (null == place || place.Deleted)
				return Json(new { result = false, msg = "巡检点无效" });

			// TODO:校验时间范围内重复打卡

			var sign = new XJPlaceSign
			{
				PlaceId = placeId,
				Latitude = latitude,
				Longitude = longitude,
				CustomerId = customerId,
				CreatedOn = DateTime.Now,
			};
			_xjPlaceSignService.InsertXJPlaceSign(sign);

			return Json(new { result = true, msg = "打卡成功" });
		}
	}
}